package com.yanqu.road.server.cmd.systemgroup.newmonopolymkt.resgrab;

import com.yanqu.road.entity.enums.eMonopolyMarketResType;
import com.yanqu.road.entity.systemgroup.ResourceServerGroup;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.newmonopolymkt.NewMonopolyMarketProto;
import com.yanqu.road.server.cmd.CrossAppointCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.manager.systemgroup.newmonopolymkt.CrossNewMonopolyMarketGroup;
import com.yanqu.road.server.manager.systemgroup.newmonopolymkt.CrossNewMonopolyMarketMgr;
import com.yanqu.road.server.manager.systemgroup.newmonopolymkt.NewMonopolyMarketResGrab;
import com.yanqu.road.server.protocol.Protocol;

import java.util.Map;

@Cmd(code = Protocol.C_CROSS_NEW_MONOPOLY_MARKET_SYN_DISPATCH_INFO, desc = "同步有变化的派遣对象信息")
public class CrossNewMonopolyMarketResGrabSynDispatchCmd extends CrossAppointCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        NewMonopolyMarketProto.NewMonopolyMarketSynDispatchToCrossReqMsg reqMsg = NewMonopolyMarketProto.NewMonopolyMarketSynDispatchToCrossReqMsg.parseFrom(packet.getBytes());
        CrossNewMonopolyMarketGroup monopolyMarket = CrossNewMonopolyMarketMgr.getMarket(groupId);
        if(monopolyMarket == null){
            return;
        }
        synchronized (monopolyMarket.getMarketResRob()) {

            for (Map.Entry<Integer, ResourceServerGroup> groupEntry : monopolyMarket.getWeekStateData().getInnerGroupList().entrySet()) {
                ResourceServerGroup serverGroup = groupEntry.getValue();
                if(serverGroup.getServerList().size() > 0 && !serverGroup.getServerList().contains(serverId)){
                    continue;
                }
                NewMonopolyMarketResGrab resGrab = monopolyMarket.getMarketResRob().getMarketResGrabMap().get(groupEntry.getKey());
                if(resGrab == null){
                    continue;
                }
                if (reqMsg.getResGrabPatronsListCount() > 0 && serverGroup.getResType() == eMonopolyMarketResType.RES_KING_NEW.getValue()) {
                    monopolyMarket.getMarketResRob().synPatrons(resGrab, packet.getPlayerId(), reqMsg.getResGrabPatronsListList());
                }
                if (reqMsg.getResGrabBeautyListCount() > 0 && serverGroup.getResType() == eMonopolyMarketResType.RES_QUEEN_NEW.getValue()) {
                    monopolyMarket.getMarketResRob().synBeauty(resGrab, packet.getPlayerId(), reqMsg.getResGrabBeautyListList());
                }
            }
        }
    }

    @Override
    public void sendData() {

    }

}
